# coding=utf-8

import argparse
import random
import base64

__src = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
__valid_len = {16, 24, 32}


def make_key(lenth):
    c_len = int(lenth)
    selected = []
    src_len = len(__src)
    for i in range(c_len):
        index = random.randint(0, src_len-1)
        selected.append(__src[index])
    return "".join(selected)


if __name__ == "__main__":
    help_msg = r"""
    usage: python aeskey.py --len=16
    len can be {16, 24, 32}
    """
    parser = argparse.ArgumentParser(description=help_msg)
    parser.add_argument('--len', type=int, help=u'Input key length')

    args = parser.parse_args()
    length = args.len
    if length not in __valid_len:
        err = "Valid key len is: {}".format(__valid_len)
        raise ValueError(err)
    key = make_key(args.len)
    key = base64.b64encode(key.encode('utf-8'))
    print(key.decode('utf-8'))

